/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frcap_insert_task.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/29 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_mng.h"
#include "frtask_mng.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}


int __frcap_insert_task(Conntrack *p_st_ct
        , u_short us_proto
        , u_int ui_src, u_short us_sport
        , u_int ui_dest, u_short us_dport
        , Franalyz_cmd em_analyz_cmd
        , char *p_data, unsigned int ui_data_len
        , int i_l7_proto
        , struct timeval st_timeval_pkg);

static void case1(void)
{
    Conntrack *p_st_ct = NULL;
    u_int ui_src = iptools_parse_ipaddr("192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 22;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_SYN;
    char *p_data = frstrdup("test");
    unsigned int ui_data_len = 4;
    int i_l7_proto = 1;
    struct timeval st_timeval_pkg = {500,50};
    int i_ret = 0;

    i_ret = __frcap_insert_task(p_st_ct, 1, ui_src, us_sport, ui_dest, us_dport
                                , em_analyz_cmd, p_data, ui_data_len, i_l7_proto, st_timeval_pkg);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);

    frfree(p_data);
    return;
}

static void case2(void)
{
    Conntrack *p_st_ct = NULL;
    u_int ui_src = iptools_parse_ipaddr("192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 22;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_DATA;
    char *p_data = NULL;
    unsigned int ui_data_len = 0;
    int i_l7_proto = 1;
    struct timeval st_timeval_pkg = {500,50};
    int i_ret = 0;

    p_st_ct = (Conntrack*)frmalloc(sizeof(Conntrack));
    memset(p_st_ct,0x00,(sizeof(Conntrack)));
    p_st_ct->dst_ip = iptools_parse_ipaddr("192.168.1.1");
    p_st_ct->dst_port = 80;

    i_ret = __frcap_insert_task(p_st_ct, 1, ui_src, us_sport, ui_dest, us_dport
                                , em_analyz_cmd, p_data, ui_data_len, i_l7_proto, st_timeval_pkg);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_st_ct);
    return;
}

static void case3(void)
{
    Conntrack *p_st_ct = NULL;
    u_int ui_src = iptools_parse_ipaddr("192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 22;
    Franalyz_cmd em_analyz_cmd = FRANALYZ_CMD_SYN;
    char *p_data = frstrdup("test");
    unsigned int ui_data_len = 4;
    int i_l7_proto = 1;
    struct timeval st_timeval_pkg = {500,50};
    int i_ret = 0;

    p_st_ct = (Conntrack*)frmalloc(sizeof(Conntrack));
    memset(p_st_ct,0x00,(sizeof(Conntrack)));
    p_st_ct->dst_ip = iptools_parse_ipaddr("192.168.1.1");
    p_st_ct->dst_port = 80;

    i_ret = __frcap_insert_task(p_st_ct, 1, ui_src, us_sport, ui_dest, us_dport
                                , em_analyz_cmd, p_data, ui_data_len, i_l7_proto, st_timeval_pkg);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frlist_remove_all(&p_st_ct->lst_cap_queue,frcap_node_free);
    frtask_mng_destory();
    frfree(p_st_ct);
    frfree(p_data);
    return;
}




int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frcap_insert_task 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}